import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [
  {
    path: "/login",
    component: () => import("@/views/Login.vue"),
    name: "login"
  },
  {
    path: "/home",
    component: () => import("@/views/Home.vue"),
    name: "home",
    children:[
      {
        path: "/users",
        component: () => import("@/components/users/users.vue"),
        name: "users"
      },
      {
        path: "/rights",
        component: () => import("@/components/rights/rights.vue"),
        name: "rights"
      },
      {
        path: "/roles",
        component: () => import("@/components/rights/roles.vue"),
        name: "roles"
      },
      {
        path: "/goods",
        component: () => import("@/components/goods/goods.vue"),
        name: "goods"
      },
      {
        path: "/goods/add",
        component: () => import("@/components/goods/add.vue"),
        name: "add"
      },
      {
        path: "/categories",
        component: () => import("@/components/goods/categories.vue"),
        name: "categories"
      },
      {
        path: "/params",
        component: () => import("@/components/goods/params.vue"),
        name: "params"
      },
      {
        path: "/orders",
        component: () => import("@/components/order/orders.vue"),
        name: "orders"
      },
      {
        path: "/reports",
        component: () => import("@/components/report/reports.vue"),
        name: "reports"
      }
    ]
  },
  {
    path: "/",
    redirect: localStorage.getItem("redirect") ? localStorage.getItem("redirect"):"/login"
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})
import store from "@/store"
router.beforeEach((to,from,next)=>{
  if(to.path=="/login"){
   next()
  }else{
    if (store.getters.token){
      next()
    }else{
      next("/login")
    }
  }
})
export default router
